package nxu.it.controller;

import io.undertow.util.StatusCodes;
import nxu.it.dao.Impl.JdbiRegisterStorage;
import nxu.it.dao.RegisterStorage;
import nxu.it.model.ApiResult;
import nxu.it.service.LoginService;
import nxu.it.model.RegisterRequest;
import nxu.it.security.NeedLoginHandler;
import nxu.it.service.GreetingService;
import nxu.it.service.LoginService;
import org.noear.solon.annotation.*;
import org.noear.solon.boot.web.MimeType;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.ModelAndView;
import static nxu.it.security.NeedLoginHandler.LOGIN_SESSION_FIELD;
import static nxu.it.security.NeedLoginHandler.REDIRECT_SESSION_FILED;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MainController {
    private static RegisterStorage registerStorage = new JdbiRegisterStorage();
    @Inject
    GreetingService greetingService;

    @Mapping(value = "/", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public String index() {
        return "<h1>Yes, it works!</h1>";
    }

    @Mapping(value = "/hello", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public String hello(String name) {
        return greetingService.sayHello(name);
    }

    @Mapping(value = "/json", produces = MimeType.APPLICATION_JSON_UTF8_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public ApiResult json() {
        return ApiResult.ok("请求成功");
    }

    @Mapping(value = "/render", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public ModelAndView render() {
        ModelAndView mv = new ModelAndView("test.ftl");
        mv.put("name", "张三");
        return mv;
    }
    @Mapping(value="/login",produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public ModelAndView login(Context ctx){
        String username = ctx.session(LOGIN_SESSION_FIELD,String.class);
        ModelAndView mv = new ModelAndView("login.ftl");
        return mv;
//        if(username == null || username.isEmpty()){
//            return new ModelAndView("login.ftl");
//        }
//        else {
//            mv.view("welcome.ftl");
//            mv.put("username",username);
//        }
    }

    @Mapping(value = "/logout",produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public void logout(Context ctx){
        ctx.sessionClear();
        ctx.redirect("/login",303);
    }

    @Mapping(value = "/loginCheck",produces = MimeType.TEXT_HTML_VALUE)
    @Post
    public void loginCheck(String username, String password,Context ctx){
        LoginService loginCheck = new LoginService();
        if(!loginCheck.check(username, password)){
            ctx.output("用户名或密码错误");
            return;
        }
        ctx.sessionSet(LOGIN_SESSION_FIELD,username);
        String redirectUrl = ctx.session(REDIRECT_SESSION_FILED,String.class);
        if(redirectUrl != null){
            ctx.redirect("/todo/list",303);
        }
        else{
            ctx.redirect("/todo/list", StatusCodes.SEE_OTHER);
        }

    }

    @Mapping(value = "/register",produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public ModelAndView register() {
        return new ModelAndView("register.ftl");
    }

    @Mapping(value = "/doRegister",produces = MimeType.TEXT_HTML_VALUE)
    @Post
    public String doRegister(RegisterRequest registerRequest) {
        if(registerStorage.submit(registerRequest)) return "注册成功";
        else return "注册失败";
    }
}